﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PCR.Base;
using PCR.Logic;

namespace PCR
{
    public partial class FormSettings : DevComponents.DotNetBar.Office2007Form
    {
        uint _baseCellCount = PCRRoot.Instance.VirusTypeManager.BaseCellCount;
        double _virusSafetyExponent = PCRRoot.Instance.VirusTypeManager.VirusSafetyExponent;

        bool _virusParameterChanged = false;
        bool _userChanged = false;
        bool _printingSettingChaned = false;

        IVirusTypeManager _virusTypeManager = null;

        public FormSettings()
        {
            InitializeComponent();

            this._virusTypeManager = PCRRoot.Instance.VirusTypeManager;
        }

        private void FormSettings_Load(object sender, EventArgs e)
        {
            // Load saved settings
            FillVirusTypesPage();
            FillRevisedParameterPage();
            FillUserPage();
            FillPrinting();

            this.textVirusTypeName.Focus();
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this._virusParameterChanged)
            {
                PCRRoot.Instance.VirusTypeManager.BaseCellCount = this._baseCellCount;
                PCRRoot.Instance.VirusTypeManager.VirusSafetyExponent = this._virusSafetyExponent;
            }

            if (this._userChanged)
            {
                if (this.textUserName.Text.Length <= 0)
                {
                    MessageBox.Show("用户名不能为空！");
                    this.textUserName.Focus();
                    return;
                }

                if (this.textOldPassword.Text.Length <= 0)
                {
                    MessageBox.Show("旧密码不能为空！");
                    this.textOldPassword.Focus();
                    return;
                }

                if (this.textNewPassword.Text.Length <= 0)
                {
                    MessageBox.Show("新密码不能为空！");
                    this.textNewPassword.Focus();
                    return;
                }

                if (this.textNewPassword.Text != textConfirmNewPassword.Text)
                {
                    MessageBox.Show("两次密码输入不一致！");
                    this.textNewPassword.Focus();
                    this.textNewPassword.SelectAll();
                    return;
                }
                PCRRoot.Instance.PrivilegeManager.UpdateUser(this.textUserName.Text, this.textNewPassword.Text);
            }
            
            if (this._printingSettingChaned)
            {
                PCRRoot.Instance.ConfigManager.PrintingSetting =
                    this.propertyDefaultValue.SelectedObject as IPrintingSetting;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
        
        private void tabSettings_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.tabSettings.SelectedTabIndex;
            if (index == 0)
            {
                this.textVirusTypeName.Focus();
                this.textVirusTypeName.SelectAll();
            }
            else if (index == 1)
            {
                this.textBaseCellCount.Focus();
                this.textBaseCellCount.SelectAll();
            }
            else if (index == 2)
            {
                this.textUserName.Text = "";
                this.textOldPassword.Text = "";
                this.textNewPassword.Text = "";
                this.textConfirmNewPassword.Text = "";
                this.textUserName.Focus();
            }
        }

#region Virus Type
        private void btnAddVirusType_Click(object sender, EventArgs e)
        {
            if (IsVirusNameExisted())
            {
                MessageBox.Show("名称已经存在", "错误",MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            IVirusType virusType = GetVirusTypeFromInput();
            if (virusType == null)
                return ;

            AddItemFromVirusType(virusType);
            this._virusTypeManager.Add(virusType);
        }

        bool IsVirusNameExisted()
        {
            foreach(ListViewItem item in this.listVirusTypes.Items)
            {
                IVirusType virusType = item.Tag as IVirusType;
                if (string.Compare(this.textVirusTypeName.Text, virusType.Name, true) == 0)
                    return true;
            }

            return false;
        }

        private void btnModifyVirusType_Click(object sender, EventArgs e)
        {
            if (this.listVirusTypes.SelectedIndices.Count == 0)
                return ;

            IVirusType virusType = this.listVirusTypes.SelectedItems[0].Tag as IVirusType;
            if (virusType == null)
                return;

            IVirusType virusTypeTemp = GetVirusTypeFromInput();
            if(virusTypeTemp == null)
                return;

            bool virusNameExisted = false;
            foreach (ListViewItem item in this.listVirusTypes.Items)
            {
                if (item == this.listVirusTypes.SelectedItems[0])
                    continue;

                IVirusType virusType1 = item.Tag as IVirusType;
                if (virusType1 == null)
                    continue;

                if (string.Compare(virusTypeTemp.Name, virusType1.Name, true) == 0)
                {
                    virusNameExisted = true;
                    break;
                }
            }

            if (virusNameExisted)
            {
                MessageBox.Show("名称已经存在", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                
                return;
            }

            
            virusType.Name = virusTypeTemp.Name;
            virusType.EVal = virusTypeTemp.EVal;
            virusType.KVal = virusTypeTemp.KVal;
            virusType.BVal = virusTypeTemp.BVal;
            virusType.VirusDefineType = virusTypeTemp.VirusDefineType;

            this._virusTypeManager.Modify(virusType);
            SetItemFromVirusType(this.listVirusTypes.SelectedItems[0], virusType);
        }
        
        private void btnDelVirusType_Click(object sender, EventArgs e)
        {
            if (this.listVirusTypes.SelectedIndices.Count == 0)
                return ;

            IVirusType type = this.listVirusTypes.SelectedItems[0].Tag as IVirusType;
            if (type == null) return;

            if (DialogResult.Yes == MessageBox.Show(
                string.Format("确定删除病毒类型 : {0}?", type.Name),
                "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                this._virusTypeManager.RemoveByID(type.ObjectID);
                this.listVirusTypes.Items.RemoveAt(this.listVirusTypes.SelectedIndices[0]);    
            }
       }

        IVirusType GetVirusTypeFromInput()
        {
            IVirusType virusType = PCRRoot.Instance.CreateObject(ObjectType.VirusType) as IVirusType;
            if (string.IsNullOrEmpty(this.textVirusTypeName.Text))
            {
                MessageBox.Show("名称不能为空", "错误", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textVirusTypeName.Focus();
                this.textVirusTypeName.SelectAll();
                
                return null;
            }
            virusType.Name = this.textVirusTypeName.Text;

            double virusEVal = 0.0;
            if (!double.TryParse(this.textVirusEVal.Text, out virusEVal))
            {
                MessageBox.Show("无效的扩增效率", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textVirusEVal.Focus();
                this.textVirusEVal.SelectAll();
                
                return null;
            }
            virusType.EVal = virusEVal;

            double virusKVal = 0.0;
            if (!double.TryParse(this.textBox1.Text, out virusKVal))
            {
                MessageBox.Show("无效的参数", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textBox1.Focus();
                this.textBox1.SelectAll();

                return null;
            }
            virusType.KVal = virusKVal;

            double virusBVal = 0.0;
            if (!double.TryParse(this.textBox2.Text, out virusBVal))
            {
                MessageBox.Show("无效的参数", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textBox2.Focus();
                this.textBox2.SelectAll();

                return null;
            }
            virusType.BVal = virusBVal;

            if (this.comboBox1.Text.Length <= 0)
            {
                MessageBox.Show("类型未选择", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.comboBox1.Focus();

                return null;
            }
            virusType.VirusDefineType = PCRRoot.VirusDefineTypeValue(this.comboBox1.Text);

            return virusType;
        }
        
        private void FillVirusTypesPage()
        {
            if (this._virusTypeManager == null) return;

            foreach(IVirusType virusType in this._virusTypeManager.List)
            {
                AddItemFromVirusType(virusType);
            }
        }

        void AddItemFromVirusType(IVirusType virusType)
        {
            ListViewItem item = new ListViewItem(virusType.Name);
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            item.SubItems.Add("");
            SetItemFromVirusType(item, virusType);            
            this.listVirusTypes.Items.Add(item);
        }

        void SetItemFromVirusType(ListViewItem item, IVirusType virusType)
        {
            item.SubItems[0].Text = virusType.Name;
            item.SubItems[1].Text = virusType.EVal.ToString();
            item.SubItems[2].Text = virusType.KVal.ToString();
            item.SubItems[3].Text = virusType.BVal.ToString();
            item.SubItems[4].Text = PCRRoot.VirusDefineTypeString(virusType.VirusDefineType);
            item.Tag = virusType;
        }

        private void listVirusTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listVirusTypes.SelectedIndices.Count == 0)
                return;

            ListViewItem item = this.listVirusTypes.SelectedItems[0];
            IVirusType virusType = item.Tag as IVirusType;
            this.textVirusTypeName.Text = virusType.Name;
            this.textVirusEVal.Text = virusType.EVal.ToString();
            this.textBox1.Text = virusType.KVal.ToString();
            this.textBox2.Text = virusType.BVal.ToString();
            this.comboBox1.Text = PCRRoot.VirusDefineTypeString(virusType.VirusDefineType);
        }

        
#endregion

#region Revised Parameter

        private void FillRevisedParameterPage()
        {
            this.textBaseCellCount.Text = this._baseCellCount.ToString();
            this.textVirusSafetyExponent.Text = this._virusSafetyExponent.ToString();
        }

        private void textBaseCellCount_TextChanged(object sender, EventArgs e)
        {
            this._virusParameterChanged = true;
        }

        private void textBaseCellCount_Leave(object sender, EventArgs e)
        {
            uint baseCellCount = 0;
            if (!uint.TryParse(this.textBaseCellCount.Text, out baseCellCount))
            {
                MessageBox.Show("无效的细胞数量", "错误", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.textBaseCellCount.Focus();
                this.textBaseCellCount.SelectAll();
            }

            this._baseCellCount = baseCellCount;
        }

        private void textVirusSafetyExponent_Leave(object sender, EventArgs e)
        {
            double virusSafetyExponent = 0.0;
            if (!double.TryParse(this.textVirusSafetyExponent.Text, out virusSafetyExponent))
            {
                MessageBox.Show("无效的感染单位阀值", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.textBaseCellCount.Focus();
                this.textBaseCellCount.SelectAll();
            }

            if (virusSafetyExponent < 0.0)
            {
                MessageBox.Show("无效的感染单位阀值", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.textBaseCellCount.Focus();
                this.textBaseCellCount.SelectAll();
            }
            
            this._virusSafetyExponent = virusSafetyExponent;
        }

        private void textVirusSafetyExponent_TextChanged(object sender, EventArgs e)
        {
            this._virusParameterChanged = true;
        }

#endregion
        
#region Privilege 

        private void FillUserPage()
        {

        }

        private void textUserName_TextChanged(object sender, EventArgs e)
        {
            this._userChanged = true;
        }

        private void textOldPassword_TextChanged(object sender, EventArgs e)
        {
            this._userChanged = true;
        }

        private void textNewPassword_TextChanged(object sender, EventArgs e)
        {
            this._userChanged = true;
        }

        private void textConfirmNewPassword_TextChanged(object sender, EventArgs e)
        {
            this._userChanged = true;
        }

#endregion
        
#region Printing Setting

        private void FillPrinting()
        {
            // Set the printing setting
            this.propertyDefaultValue.SelectedObject =
                PCRRoot.Instance.ConfigManager.PrintingSetting;
        }

        private void propertyDefaultValue_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            this._printingSettingChaned = true;
        }

#endregion
    }
}
